package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.result.EmployeeQueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.service.impl.EmployeeServiceImpl;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.hssf.usermodel.HSSFAnchor;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * @Description: TODO
 * @Author: 伍家钰
 * @Date: 2020/7/10 21:12
 * @Version: 1.0
 */
@Controller
@RequestMapping("/employee")
public class  EmployeeController {
    @Autowired
    private IEmployeeService employeeServiceImpl;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleServiceImpl;


    /**
     * 删除操作
     *
     * @param id:
     * @return :
     */
    @RequestMapping("/delete")
    public String delete(Long id) {
        // 完善后面可以做个判断
        if (id != null) {
            employeeServiceImpl.delete(id);
        }
        return "redirect:/employee/list.do";
    }

    /**
     * 去新增或者修改页面
     *
     * @param id:
     * @param model:
     * @return :
     */
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        model.addAttribute("role", roleServiceImpl.selectAll());
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments", departments);
        if (id != null) {
            Employee employee = employeeServiceImpl.selectById(id);
            model.addAttribute("employee", employee);
        }
        return "employee/input";
    }

    /**
     * 新增或者修改
     *
     * @param employee:前端出入的employee对象
     * @param ids:用户选择的权限的id
     * @return :
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult update(Employee employee, Long[] ids) {
        if (employee.getId() != null) {
            employeeServiceImpl.update(employee, ids);
        } else {
            employeeServiceImpl.save(employee, ids);
        }
        //让浏览器重新发送一次请求
        return new JsonResult();
    }

    /**
     * 查询操作
     *
     * @param model:
     * @param qo:
     * @return :
     */
    @RequestMapping("/list")
    public String query(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageInfo<Employee> pageInfo = employeeServiceImpl.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments", departments);
        return "employee/list";
    }

    /**
     * 批量删除
     */
    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete(@Param("ids") Long[] ids) {
        // 删除员工,当id为空时,提示删除失败
        employeeServiceImpl.batchDelete(ids);
        return new JsonResult();
    }

    /**
     * 验证用户名
     *//*
    @RequestMapping("/checkName")
    @ResponseBody
    public Map checkName(String name) {
        Employee employee = employeeServiceImpl.selectByName(name);
        HashMap map = new HashMap();
        map.put("valid", employee == null);
        return map;
    }*/

    /**
     * 导出功能
     */
    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws Exception {
        //文件下载的响应头（让浏览器访问资源的的时候以下载的方式打开）
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        //创建excel文件
        Workbook wb = employeeServiceImpl.exportXls();
        //把excel的数据输出给浏览器
        wb.write(response.getOutputStream());
    }

    /**
     * 导入功能
     */
    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws IOException {
        employeeServiceImpl.importXls(file);
        return new JsonResult();
    }

    /**
     * 设置禁用
     */
    @RequestMapping("/status")
    @ResponseBody
    public JsonResult status(Long id) {
        Employee employee = employeeServiceImpl.selectById(id);
        employeeServiceImpl.status(id, !employee.isStatus());
        return new JsonResult(true, "操作成功");
    }

    // 表单验证
    @RequestMapping("/checkName")
    @ResponseBody
    public Map checkName(Long id, String name) {
        HashMap map = new HashMap();
        //新增
        if (id == null) {
            Employee employee = employeeServiceImpl.selectByName(name);
            map.put("valid", employee == null);
        } else {
            // 用户名就是前端传入的
            Employee employee = employeeServiceImpl.selectById(id);
            if (name.equals(employee.getName())) {
                map.put("valid", true);
            } else {
                Employee employee1 = employeeServiceImpl.selectByName(name);
                map.put("valid", employee1 == null);
            }
        }
        return map;
    }
}
